//
//  UITextView+MaxWord.m
//  s.
//
//  Created by fanshijian on 2022/9/1.
//  Copyright © 2022 J. All rights reserved.
//

#import "UITextView+MaxWord.h"
#import "NSString+Category.h"

static const char *HLTextView_wordMax = "HLTextView_wordMax";
static const char *HLTextView_isBytes = "HLTextView_isBytes";
static const char *HLTextView_toast = "HLTextView_toast";

@implementation JYTextViewObject
@end

@interface UITextView () <UITextViewDelegate>

@end

@implementation UITextView (MaxWord)

- (void)setWordMax:(NSInteger)wordMax {
    self.delegate = self;
    objc_setAssociatedObject(self, HLTextView_wordMax, @(wordMax), OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}

- (NSInteger)wordMax {
    return [objc_getAssociatedObject(self, HLTextView_wordMax) doubleValue];
}

- (void)setIsBytes:(BOOL)isBytes {
    objc_setAssociatedObject(self, HLTextView_isBytes, @(isBytes), OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}

- (BOOL)isBytes {
    return [objc_getAssociatedObject(self, HLTextView_isBytes) boolValue];
}

- (void)setToastMsg:(NSString *)toastMsg {
    objc_setAssociatedObject(self, HLTextView_toast, toastMsg, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}

- (NSString *)toastMsg {
    return objc_getAssociatedObject(self, HLTextView_toast);
}

- (void)setChangeBlock:(JYChangeTextBlock)changeBlock {
    objc_setAssociatedObject(self, _cmd, changeBlock, OBJC_ASSOCIATION_COPY);
}

- (JYChangeTextBlock)changeBlock {
    return objc_getAssociatedObject(self, @selector(setChangeBlock:));
}

- (void)setObj:(JYTextViewObject *)obj {
    objc_setAssociatedObject(self, _cmd, obj, OBJC_ASSOCIATION_RETAIN);
}

- (JYTextViewObject *)obj {
    JYTextViewObject *object = objc_getAssociatedObject(self, @selector(setObj:));
    if (!object) {
        JYTextViewObject *a = [JYTextViewObject new];
        [self setObj:a];
        return a;
    }
    return object;
}


#pragma mark -- UITextFieldDelegate
// 监听文本改变
- (void)textFieldDidChange:(UITextField *)textField {
    NSString* toBeString = self.text;
    [self cutTextFieldString:toBeString bytesLength:self.wordMax];
}

- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text {
    BOOL is = YES;
    if (self.obj.delegate && [self.obj.delegate respondsToSelector:@selector(textView:shouldChangeTextInRange:replacementText:)]) {
        is = [self.obj.delegate textView:textView shouldChangeTextInRange:range replacementText:text];
    }
    if (is) {
        NSString* toBeString = [self.text stringByReplacingCharactersInRange:range withString:text];
        if (self.isBytes) {
            NSUInteger charLen = toBeString.byteLength;
            if (charLen > self.wordMax) {
                if (![text isEqualToString:@""]) {
                    [self showToast];
                    if (self.text.byteLength != self.wordMax) {
                        //如果输入的最后一个字符是emoji, 且输入后超过最大限制字符编码,则return
                        if (self.text.byteLength + text.byteLength > self.wordMax && [text stringContainsEmoji]) {
                            return NO;
                        }
                        
                        [self cutTextFieldString:toBeString bytesLength:self.wordMax];
                    }
                    return NO;
                }
            }
        }else {
            if (toBeString.length > self.wordMax) {
                [self showToast];
                return NO;
            }
        }
    }
    return is;
}

- (void)cutTextFieldString:(NSString *)textString bytesLength:(NSInteger)maxLength {
    while (textString.byteLength > maxLength) {
        textString = [textString substringToIndex:textString.length - 1];
        self.text = textString;
    }
}

- (void)showToast {
    if (self.toastMsg && self.toastMsg.length > 0) {
        toast(self.toastMsg);
    }
}

#pragma mark - 代理传出去
- (void)textViewDidChange:(UITextView *)textView {
    if (self.changeBlock) {
        self.changeBlock(textView);
    }
    
    if (self.obj.delegate && [self.obj.delegate respondsToSelector:@selector(textViewDidChange:)]) {
        [self.obj.delegate textViewDidChange:textView];
    }
}

@end
